const Koa = require('koa')
const koaBody = require('koa-body')
const cors = require('koa2-cors')
const logger = require('koa-logger')
const config = require('./config')
const router = require('./router')
const initSocket = require('./socket-server')

const app = new Koa()

app
  .use(cors())
  .use(
    koaBody({
      multipart: true,
      formidable: {
        // uploadDir: path.resolve(__dirname, './upload'),
        keepExtensions: true, // 保持文件的后缀
        maxFileSize: 2000 * 1024 * 1024 // 设置上传文件大小最大限制，默认20M
      }
    })
  )
  .use(logger())

app.use(router.routes(), router.allowedMethods())

initSocket(app)

app.listen(config.server_port, () => {
  console.log(`server listen on http://localhost:${config.server_port}`)
})
